package com.jdbc.conn;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @author wzm
 * @create 2023-02-23 10:03
 */
//连接mysql 1
public class Conn02 {
    public static void main(String[] args) throws Exception {
        //加载文件  1 通过类加载器加载文件流  仅仅适用于非web项目
        //InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        //加载文件 2 当前类.class加载文件作为流
        InputStream is = Conn02.class.getClassLoader().getResourceAsStream("jdbc.properties");

        //读取连接的信息
        Properties properties = new Properties();
        properties.load(is);

        //将properties中的所有键值对取出来
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");




        //1.注册驱动  调用Driver中静态代码块会注册驱动到管理器
        Class.forName(driverClass);

        //2.通过驱动管理器建立连接
        Connection conn = DriverManager.getConnection(url, user, password);

        System.out.println("conn = " + conn);
        if(conn != null){
            System.out.println("恭喜打开连接成功");
        }

    }

}
